package bar.dark.lee.fan;

import java.util.ArrayList;

import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.common.BDUtil;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class FanAdapter extends ArrayAdapter<FanData>
{
	public static final String OK = "Ｏ";
	public static final String NO = "Ｘ";
	public static final String NOOK = "△";
	
	public static final int OUT = 0;
	public static final int IN = 1;
	
	private Context context;
	private ArrayList<FanData> list;
	private int inOutFlag;
	
	private String shape;
	
	public FanAdapter(Context context, int textViewResourceId,
			ArrayList<FanData> list, int flag)
	{
		super(context, textViewResourceId, list);
		this.context = context;
		this.list = list;
		this.inOutFlag = flag;
		
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
		shape = " " + shape;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		
		if (v == null)
		{
			LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			v = li.inflate(R.layout.list_item_fan, null);
		}
		
		FanData data = list.get(position);
		
		TextView date = (TextView) v.findViewById(R.id.fanListDate);
		TextView name = (TextView) v.findViewById(R.id.fanListName);
		TextView money = (TextView) v.findViewById(R.id.fanListMoney);
		TextView check = (TextView) v.findViewById(R.id.fanListCheckSign);
		
		date.setText(data.getDate());
		name.setText(data.getName());
		
		if (data.getId().equals("-1"))
		{
			v.setBackgroundColor(context.getResources().getColor(R.color.bankbookcolor));
			LinearLayout.LayoutParams params = (LayoutParams) name.getLayoutParams();
			params.weight = 2;
			
			date.setVisibility(View.GONE);
			name.setLayoutParams(params);
		}
		else
		{
			v.setBackgroundColor(0x00000000);
			LinearLayout.LayoutParams params = (LayoutParams) name.getLayoutParams();
			params.weight = 1;
			
			date.setVisibility(View.VISIBLE);
			name.setLayoutParams(params);
		}
		
		if (data.getCheckSign().equals("0"))
		{
			check.setText(NO);
			money.setTextColor(context.getResources().getColor(R.color.outmoney));
			check.setTextColor(context.getResources().getColor(R.color.outmoney));
		}
		else if (data.getCheckSign().equals("2"))
		{
			check.setText(NOOK);
			money.setTextColor(context.getResources().getColor(R.color.postmoney));
			check.setTextColor(context.getResources().getColor(R.color.postmoney));
		}
		else
		{
			check.setText(OK);
			money.setTextColor(context.getResources().getColor(R.color.inmoney));
			check.setTextColor(context.getResources().getColor(R.color.inmoney));
		}
		
		money.setText(BDUtil.getWon(data.getMoney()) + shape);
		
		return v;
	}
}
